<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../global/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../global/layui/css/layui.css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal" onsubmit="return false" id="form" enctype="multipart/form-data">
        <fieldset>
     <input type="hidden" id="id" name="id"/>
          <div class="form-group">
           <label class="col-md-2 control-label">代理商账号:</label>
           <div class="col-md-5">
            <input class="form-control" placeholder="代理商账号" type="text" name="phone" id="phone" data-bv-notempty="true"  data-bv-notempty-message="代理商账号不能为空" />
            <button class="layui-btn layui-btn-sm layui-btn-normal" id="btns" onclick="seach()">查找</button>
           </div>
          </div>
          <div class="form-group">
           <label class="col-md-2 control-label">代理商类型:</label>
           <div class="col-md-5">
           <label><input id="agentFlag" name="agentFlag" onclick="off()" type="radio" value="1" />地市代理</label>
           <label><input id="agentFlag" name="agentFlag" onclick="off()" type="radio" value="2" />县区代理</label>
           </div>
           </div>

         <div class="form-group">
                    <label class="col-md-2 control-label">商家授权数量:</label>
                    <div class="col-md-5">
                     <input class="form-control" placeholder="代理商账号" type="text" name="shopsSum" id="shopsSum" data-bv-notempty="true" data-bv-notempty-message="代理商账号不能为空" />
                    </div>
                   </div>

          <div class="form-group">
            <label class="col-md-2 control-label">代理地区:</label>
            <div class="col-md-10" style="width: 120px">
            <select class="form-control input-sm" name="s_province" id="s_province"></select>
            <select class="form-control input-sm" name="s_city" id="s_city"></select>
            <select class="form-control input-sm" name="s_county" id="s_county"></select>
           </div>
        </div>
            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="layui-btn layui-btn-lg layui-btn-normal" id="btn"type="submit" onclick="update()">确认
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>
    </form>

</div>
<script type="text/javascript" src="../../global/js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../global/js/jq.js"></script>
<script type="text/javascript" src="../../global/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../global/js/common.js"></script>
<script type="text/javascript" src="../../global/layui/layui.js"></script>
<script type="text/javascript" src="../../global/js/ajaxfileupload.js"></script>
<script type="text/javascript" src="../../global/js/area.js"></script>
<script type="text/javascript">
 layui.use('layer', function () {
        var layer = layui.layer;
    });
 _init_area();
function seach(){
    var phone=$("#phone").val();
    $.ajax({
              type: 'get',
              url: "/clyjUserAgent/seach/" + phone,
              async: false,
              success: function (result) {
              var data = result.data;
              if(data==null){
layer.open({
  title: '提示'
  ,content: '该账号信息有误'
});
  $("#btn").attr("class","layui-btn layui-btn-lg layui-btn-disabled");
              }else {
layer.open({
  title: '提示'
  ,content: '该账号可以设置代理'
});
  $("#btn").attr("class","layui-btn layui-btn-lg layui-btn-normal");
              }
                    }
    });

}
function off(){
$("#btn").removeAttr("disabled","disabled");
}
    initData();
    function initData() {
        var id = getUrlParam("id");
        if (id != "") {
    $("#btns").attr("class","layui-btn layui-btn-sm layui-btn-disabled");
            $.ajax({
                type: 'get',
                url: "/clyjUserAgent/" + id,
                async: false,
                success: function (result) {
                    var data = result.data;
                     $("#id").val(data.id);
                     $("#phone").val(data.phone);
                     $("input[type=radio]").attr("checked",data.agentFlag);
                     $("#shopsSum").val(data.shopsSum);
                     if(data.city!=null){
                          $("#s_city").append("<option selected>"+data.city+"</option>");
                     }if(data.county!=null){
                         $("#s_county").append("<option selected>"+data.county+"</option>");
                     }


                }
            });
           //initRoleDatas(id);
        }
    }

    $('#form').bootstrapValidator();

    function update() {
      //判断radio是否选择
      var s= $('input:radio:checked').val();
        if(s==null ||s==""){
          layer.open({
            title: '提示'
            ,content: '您还未选择代理商类型'
          });
          return ;
        }
        if(s=="1"){
            var s_city=$('#s_city option:selected').val();
           if(s_city=="地级市"){
              layer.open({
                          title: '提示'
                          ,content: '您还未选择市代理地区,请重新选择代理商类型'
                        });

                        return;
           }

        }
         if(s=="2"){
                    var s_county=$('#s_county option:selected').val();
                   if(s_county=="区、县级市"){
                                                  layer.open({
                                                              title: '提示'
                                                              ,content: '您还未选择县代理地区,请重新选择代理商类型'
                                                            });
                                                            return;
                                               }

                }

             // 表单验证
                var bootstrapValidator = $("#form").data('bootstrapValidator');
                bootstrapValidator.validate();
                if (!bootstrapValidator.isValid()) {
                    return;
                }
        $("form").ajaxSubmit({
            type: 'post',
            url: '/clyjUserAgent/updatebyagent',
            traditional: true,
            contentType: "application/json; charset=utf-8",
            //data: JSON.stringify(formdata),
            success: function (data) {
                layer.msg("修改成功", {shift: -1, time: 1000}, function () {
                    location.href = "agentList.html";
                });
            }
        });
    }

</script>
</body>
</html>